/*
Copyright (c) 2010 Yunus Kara

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "StdAfx.h"
#include "PhysicsSystem.h"
#include "GraphicsSystem.h"
using namespace Ogre;

template<> PhysicsSystem* Ogre::Singleton<PhysicsSystem>::ms_Singleton = 0;

PhysicsSystem* PhysicsSystem::getSingletonPtr(void)
{
	return ms_Singleton;
}

PhysicsSystem& PhysicsSystem::getSingleton(void)
{  
	assert( ms_Singleton );  return ( *ms_Singleton );
}

PhysicsSystem::PhysicsSystem() : gPhysicsSDK(NULL), gScene(NULL), mVisualDebugger(NULL), mVisualDebuggerNode(NULL)
{
	mDebuggerView = false;
}

PhysicsSystem::~PhysicsSystem()
{
	Finalize();
}

void PhysicsSystem::Initialize()
{
	//sdk
	gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);

	//scene
	NxSceneDesc sceneDesc;
	sceneDesc.gravity = NxVec3(0,-10,0);
	sceneDesc.simType = NX_SIMULATION_SW;
	sceneDesc.userContactReport = &gContactReport;
	gPhysicsSDK->getFoundationSDK();
	gScene = gPhysicsSDK->createScene(sceneDesc);

	float timestep = 1.0f/100.0f;
	gScene->setTiming(timestep, 8, NX_TIMESTEP_FIXED);

	//props.
	gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.01);
	gPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 1);
	gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
	gPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);
	gPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_LOCAL_AXES, 10);

	//set default mat
	NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0);
	defaultMaterial->setRestitution(0.25);
	defaultMaterial->setStaticFriction(0.60);
	defaultMaterial->setDynamicFriction(0.40);

	//char contrroller manager
	ControllerAllocator* alloc = new ControllerAllocator();
	gManager = NxCreateControllerManager(alloc);

	//debugger
	mVisualDebugger = GSYS->GetSceneMgr()->createManualObject("mVisualDebugger");
	mVisualDebugger->setCastShadows(false);
	mVisualDebuggerNode = GSYS->GetSceneMgr()->getRootSceneNode()->createChildSceneNode();
	mVisualDebuggerNode->attachObject(mVisualDebugger);
	MaterialPtr mVisualDebuggerMaterial = MaterialManager::getSingleton().create("mVisualDebuggerMaterial","General"); 
	mVisualDebuggerMaterial->setReceiveShadows(false); 
	mVisualDebuggerMaterial->getTechnique(0)->setLightingEnabled(false);
	mVisualDebugger->clear();
	mVisualDebugger->begin("mVisualDebuggerMaterial", Ogre::RenderOperation::OT_LINE_LIST); 
	mVisualDebugger->position(Vector3(0,0,0));
	mVisualDebugger->position(Vector3(0,1.8,0));
	mVisualDebugger->position(Vector3(0,1.8,0));
	mVisualDebugger->position(Vector3(4,1.8,0));
	mVisualDebugger->position(Vector3(0,1.8,0));
	mVisualDebugger->position(Vector3(0,1.8,2));
	mVisualDebugger->end();
	AxisAlignedBox aabInf;
	aabInf.setInfinite();
	mVisualDebugger->setBoundingBox(aabInf);
}

void PhysicsSystem::Finalize()
{
	//debugger
	mVisualDebuggerNode->detachAllObjects();
	mVisualDebuggerNode->getParentSceneNode()->removeAndDestroyChild(mVisualDebuggerNode->getName());
	mVisualDebuggerNode = NULL;
	mVisualDebugger->clear();
	GSYS->GetSceneMgr()->destroyManualObject(mVisualDebugger);
	mVisualDebugger = NULL;

	//phy
	NxReleaseControllerManager(gManager);
	gScene->fetchResults(NX_RIGID_BODY_FINISHED,true);
	gPhysicsSDK->releaseScene(*gScene);
	gPhysicsSDK->release();
}

void PhysicsSystem::Update()
{
	gScene->simulate(GlobalVars::Tick);
	gScene->flushStream();
	while(!gScene->fetchResults(NX_RIGID_BODY_FINISHED, true)){}

	//debug
	if (mDebuggerView)
	{
		const NxDebugRenderable* data = gScene->getDebugRenderable();
		NxU32 NbLines = NULL;
		if (data)NbLines = data->getNbLines();
		const NxDebugLine* Lines;
		if (data)Lines = data->getLines();
		
		mVisualDebugger->clear();
		mVisualDebugger->begin("mVisualDebuggerMaterial", Ogre::RenderOperation::OT_LINE_LIST);
		while(NbLines--)
		{
			mVisualDebugger->position(Lines->p0.x,Lines->p0.y,Lines->p0.z);
			mVisualDebugger->colour(1,1,1);
			mVisualDebugger->position(Lines->p1.x,Lines->p1.y,Lines->p1.z);
			mVisualDebugger->colour(1,1,1);
			Lines++;
		}
		mVisualDebugger->end();
	}
}

void PhysicsSystem::SetActorCollisionGroup(NxActor* actor, NxCollisionGroup group)
{
    NxShape*const* shapes = actor->getShapes();
    NxU32 nShapes = actor->getNbShapes();
    while (nShapes--)
    {
        shapes[nShapes]->setGroup(group);
	}
}

void PhysicsSystem::FlipDebug()
{
	mDebuggerView = !mDebuggerView;
	if (!mDebuggerView)
	{
		mVisualDebugger->clear();
		mVisualDebugger->begin("mVisualDebuggerMaterial", Ogre::RenderOperation::OT_LINE_LIST); 
		mVisualDebugger->position(Vector3(0,0,0));
		mVisualDebugger->position(Vector3(0,1.8,0));
		mVisualDebugger->position(Vector3(0,1.8,0));
		mVisualDebugger->position(Vector3(4,1.8,0));
		mVisualDebugger->position(Vector3(0,1.8,0));
		mVisualDebugger->position(Vector3(0,1.8,2));
		mVisualDebugger->end();
	}
}

Ogre::Vector3 PhysicsSystem::CastRay1(Ogre::Vector3 from, Ogre::Vector3 dir)
{
	//ray cast
	NxVec3 org = PhysicsHelper::toNX(from);
	NxVec3 ndir = PhysicsHelper::toNX(dir);
	NxRay ray(org, ndir);
	NxRaycastHit hit;
	//Get the closest shape
	NxShape* closestShape = gScene->raycastClosestShape(ray, NX_STATIC_SHAPES, hit);
	if (closestShape)
	{
		const NxVec3& worldImpact = hit.worldImpact;
		return PhysicsHelper::toOgre(worldImpact);
	}
	return Vector3::ZERO;
}

Ogre::Vector3 PhysicsSystem::CastRay2(Ogre::Vector3 from, Ogre::Vector3 to, NxShape** shape, NxMaterialIndex &mat)
{
	//ray cast
	NxVec3 org = PhysicsHelper::toNX(from);
	Vector3 delta = to - from;
	NxVec3 ndir = PhysicsHelper::toNX(delta.normalisedCopy());
	NxRay ray(org, ndir);
	NxRaycastHit hit;
	mat = 0;
	//Get the closest shape
	NxShape* closestShape = gScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit, -1, delta.length());
	if (closestShape)
	{
		if (shape != NULL)*shape = closestShape;
		mat = hit.materialIndex;
		const NxVec3& worldImpact = hit.worldNormal;
		return PhysicsHelper::toOgre(worldImpact);
	}
	if (shape != NULL)*shape = NULL;
	return Vector3::ZERO;
}

Ogre::Vector3 PhysicsSystem::CastRay3(Ogre::Vector3 from, Ogre::Vector3 to)
{
	//ray cast
	NxVec3 org = PhysicsHelper::toNX(from);
	Vector3 delta = to - from;
	NxVec3 ndir = PhysicsHelper::toNX(delta.normalisedCopy());
	NxRay ray(org, ndir);
	NxRaycastHit hit;
	//Get the closest shape
	NxShape* closestShape = gScene->raycastClosestShape(ray, NX_STATIC_SHAPES, hit, -1, delta.length());
	if (closestShape)
	{
		const NxVec3& worldImpact = hit.worldImpact;
		return PhysicsHelper::toOgre(worldImpact);
	}
	return Vector3::ZERO;
}

bool PhysicsSystem::OverlapTest(Ogre::Vector3 min, Ogre::Vector3 max)
{
	NxBounds3 worldBounds;
	worldBounds.set(PhysicsHelper::toNX(min), PhysicsHelper::toNX(max));
	return gScene->checkOverlapAABB(worldBounds, NX_STATIC_SHAPES);
}

NxMaterialIndex PhysicsSystem::addNewMaterial(Ogre::String name)
{
	//check existance
	std::map<NxMaterialIndex, Ogre::String>::iterator it;
	for (it=materials.begin();it != materials.end();it++)
	{
		if (name == (*it).second)
		{
			return (*it).first;
		}
	}
	//else create new
	NxMaterialDesc mdesc;
	mdesc.restitution = 0.25;
	mdesc.staticFriction = 0.60;
	mdesc.dynamicFriction = 0.40;
	NxMaterial* mat = gScene->createMaterial(mdesc);
	NxMaterialIndex index = mat->getMaterialIndex();
	materials[index] = name;
	if (name.length() != 0) return index;
	else return 0;
}

Ogre::String PhysicsSystem::getMaterialName(NxMaterialIndex index)
{
	std::map<NxMaterialIndex, Ogre::String>::iterator it = materials.find(index);
	if (it != materials.end())
	{
		return (*it).second;
	}
	else
	{
		return "";
	}
}